package cn.com.dhcc.turbo.dao.exch;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.entity.exch.SendCmData;

@Repository
public class SendCmDataDao extends AbstractCrudDao<SendCmData> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	/*
	 * 批量更新通知状态
	 */
	public int updateNotifyStatus(List<SendCmData> dataList, String status) throws DaoException {
		String msg = "";
		try {
			String updateSql = "update " + SI_TABLE("TURBO_SEND_CM_DATA")+ " set notify_status = ?,routelog=?,sender_signed=?,modified=? ,THROUGH_GAP=?,POSTHEAD=? where id = ?";
			List<Object[]> paramList = new ArrayList<Object[]>();
			if (CollectionUtil.hasElement(dataList)) {
				for (SendCmData vo : dataList) {
					Object[] param = new Object[7];
					param[0] = status;
					param[1] = vo.getRoutelog();
					param[2] = vo.getSenderSigned();
					param[3] = new Date();
					
					param[4] = vo.getThroughGap();
	                param[5] = vo.getPosthead();
	                	
	                	
					param[6] = vo.getId();
					paramList.add(param);
					CM.getDao().update("批量修改日志状态成功", updateSql, param);
				}
			}
			return 0;//CM.getDao().batchUpdate("批量修改日志状态成功", updateSql, paramList);
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 批量更新数据发送状态
	 * @param dataList
	 */
	public int updateSendStatus(List<SendCmData> dataList, String status) throws DaoException {
		String msg = "";
		try {
			String updateSql = "update " + SI_TABLE("TURBO_SEND_CM_DATA")+ " set send_status = ?,routelog=?,reciver_signed=?,modified=?,THROUGH_GAP=?,POSTHEAD=?  where id = ?";
			List<Object[]> paramList = new ArrayList<Object[]>();
			if (CollectionUtil.hasElement(dataList)) {
				for (SendCmData vo : dataList) {
					Object[] param = new Object[7];
					param[0] = status;
					param[1] = vo.getRoutelog();
					param[2] = vo.getReciverSigned();
					param[3] = new Date();
					
				    param[4] = vo.getThroughGap();
                	param[5] = vo.getPosthead();
                	
                	
					param[6] = vo.getId();
					paramList.add(param);
				}
			}
			return CM.getDao().batchUpdate("批量修改日志状态成功", updateSql, paramList);
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取发送节点列表
	 * @return
	 */
	public List<Map<String, Object>> getSendNodeList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when node_name is null then code else node_name end name FROM( "+
					"SELECT DISTINCT send_nodecode code FROM "+SI_TABLE("turbo_send_cm_data")+" WHERE  msg_type != ? ) a  "+
					"left join "+SI_TABLE("turbo_node_info")+" b ON a.code = b.node_code ";
			String[] args = new String[] { AppConst.MsgType.GROUP_MSG.value()};
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取发送节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取接收节点列表
	 * @return
	 */
	public List<Map<String, Object>> getRecvNodeList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when node_name is null then code else node_name end name FROM( "+
					"SELECT DISTINCT recv_nodecode code FROM "+SI_TABLE("turbo_send_cm_data")+" WHERE msg_type != ? )a  "+
					"left join "+SI_TABLE("turbo_node_info")+" b ON a.code = b.node_code ";
			String[] args = new String[] { AppConst.MsgType.GROUP_MSG.value()};
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取接收节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取发送适配器列表
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when ADAPTER_NAME is null then code else ADAPTER_NAME end name FROM( "+
					"SELECT DISTINCT SEND_ADAPTORCODE code FROM "+SI_TABLE("turbo_send_cm_data")+" WHERE msg_type != ? )a  "+
					"left join "+SI_TABLE("turbo_adapter_base")+" b ON a.code = b.ADAPTER_CODE ";
			String[] args = new String[] { AppConst.MsgType.GROUP_MSG.value()};
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取接收节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	

}
